package plug.LinkWordsPlugin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import plug.Introspection;

import view.Professeurs.ProfPage;

@SuppressWarnings("serial")
public class LinkWordsPanelProf extends JPanel implements ActionListener {

	private JTextField tLeft, tRight;
	private JButton bAdd, bEnter;
	private ArrayList<String> sLeft, sRight;
	private ArrayList<StringPaire> paire;

	public LinkWordsPanelProf() {

		// Creation des conteneurs
		JPanel pZones = new JPanel();
		tLeft = new JTextField(10);
		tRight = new JTextField(10);
		bAdd = new JButton("Ajouter");
		bEnter = new JButton("Valider");
		paire = new ArrayList<StringPaire> ();
		sLeft = new ArrayList<String> ();
		sRight = new ArrayList<String> ();
		
		// Ajout de la transparence
		this.setOpaque(false);
		pZones.setOpaque(false);

		// Ajout des conteneurs au panel
		pZones.add(tLeft);
		pZones.add(tRight);
		this.add(pZones);
		this.add(bAdd);
		this.add(bEnter);

		// Ajout des ecouteurs aux boutons
		bAdd.addActionListener(this);
		bEnter.addActionListener(this);

	}

	public void actionPerformed(ActionEvent event) {

		if (event.getSource() == bAdd) {

			if (!tLeft.getText().isEmpty() && !tRight.getText().isEmpty()) {
				
				paire.add(new StringPaire(tLeft.getText(), tRight.getText()));
				
				sLeft.add(tLeft.getText());
				sRight.add(tRight.getText());
				
				tLeft.setText("");
				tRight.setText("");

			}
			else {

				String message = "Veuillez saisir un texte dans chaque zone de saisie.";
				JOptionPane.showMessageDialog(null, message, "Ajout du mot impossible", JOptionPane.ERROR_MESSAGE);

			}

		}
		else if (event.getSource() == bEnter) {

			bAdd.setEnabled(false);
			bEnter.setEnabled(false);
			
			String chaine = "";
			
			for (int i=0; i<paire.size(); i++) {
				
				chaine += paire.get(i).getFirst() + '\n' + paire.get(i).getSecond() + '\n';
				
			}

			LinkWords l = (LinkWords) Introspection.exo;
			l.creer(chaine);

			ProfPage.center.update(new JPanel());			// Effacement du panel
			String message = "Validation effectu\u00E9e";
			JOptionPane.showMessageDialog(null, message, "Validation", JOptionPane.INFORMATION_MESSAGE);

		}

	}

}
